package com.shuorikeji.huohuo.view;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Build;
import android.view.View;

import androidx.annotation.RequiresApi;

public class ViewPager2JzTransformer extends BasePageTransformer {
    private final Camera camera = new Camera();
    private final Matrix matrix = new Matrix();

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void transformPage(View page, float position) {
        resetTransform(page);
        float absPosition = Math.abs(position);

        if (position < -1 || position > 1) {
            page.setAlpha(0f);
            return;
        }

        page.setAlpha(1f);
        page.setTranslationX(-page.getWidth() * position);

        // 缩放效果
        float scale = 1 - 0.1f * absPosition;
        page.setScaleX(scale);
        page.setScaleY(scale);

        // 使用 Camera 实现 3D 旋转
        camera.save();
        matrix.reset();
        camera.rotateY(30 * position);
        camera.getMatrix(matrix);
        camera.restore();

        // 调整旋转中心
        float centerX = page.getWidth() / 2f;
        float centerY = page.getHeight() / 2f;
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);

        // 应用变换
        page.setAnimationMatrix(matrix);

        // 深度效果
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            page.setTranslationZ(-absPosition);
        }
    }
}
